
package com.trydofor.id.byta;

/**
 * the one can convert between byte array and itself.
 * 
 * @param <T> the type.
 * @author : Shi Rongjiu (www.trydofor.com)
 */
public interface Bytable<T extends Bytable<? super T>> {
    
    /**
     * make a byte array from the object.
     * 
     * @return byte array, empty array if get null.
     * @throws Exception if got an exception
     */
    byte[] toBytes() throws Exception;
    
    /**
     * make a new object from the bytes.
     * how many bytes to use is assigned with the parameter.
     * 
     * @param bytes the source byte array.
     * @param offset the first byte to use, MUST range [0,bs.length-1].
     * @param length the length of bytes to use, MUST range [1,bs.length-offset]
     * @return the object, null if can Not make.
     * @throws Exception if got an exception
     */
    T ofBytes(byte[] bytes, int offset, int length) throws Exception;
    
    /**
     * make a new object from the bytes.
     * how many bytes to use is returned by meta.
     * 
     * @param bytes the source byte array.
     * @param offset the first byte to use, MUST range [0,bs.length-1].
     * @return the meta, never return null
     * @throws Exception if got an exception
     */
    ByteMeta<T> ofBytes(byte[] bytes, int offset) throws Exception;
}
